#include "qscripteditdialog.h"

#include "qproperty.h"
#include "scripteditview.h"
#include "qfindwidget.h"
#include "qscriptparaminfowidget.h"
#include "qscriptobjectlistwidget.h"

#include "../../lib/corelib/styledbar.h"
#include "../../lib/corelib/qactiontoolbar.h"
#include "../../lib/corelib/minisplitter.h"

#include <QVBoxLayout>
#include <QApplication>
#include <QCloseEvent>
#include <QMessageBox>

QScriptEditDialog::QScriptEditDialog(QProperty *property, QWidget *parent):
    QDialog(parent),
    m_scripteView(new ScriptEditView(this)),
    m_scripteViewBar(new StyledBar(this)),
    m_toolBar(new QActionToolBar(this)),
    m_infoWidget(new QScriptParamInfoWidget(property,this)),
    m_objectListView(new QScriptObjectListWidget(this)),
    m_statusBar(new QStatusBar(this)),
    m_modified(false),
    m_property(property),
    m_findWidget(new QFindWidget(this))
{
    QVBoxLayout *l=new QVBoxLayout;
    l->setMargin(2);
    l->setSpacing(0);
    l->addWidget(m_toolBar);
    m_scripteViewBar->setLayout(l);


    l=new QVBoxLayout;
    l->setMargin(0);
    l->setSpacing(0);

    MiniSplitter *splitter=new MiniSplitter(this);
    l->addWidget(splitter);
    splitter->addWidget(m_objectListView);
    m_objectListView->resize(200,200);

    QWidget *wid=new QWidget();
    QVBoxLayout *v=new QVBoxLayout();
    v->setMargin(0);
    v->setSpacing(0);
    v->addWidget(m_scripteViewBar);
    v->addWidget(m_scripteView);
    v->addWidget(m_findWidget);
    wid->setLayout(v);

    splitter->addWidget(wid);
    splitter->setStretchFactor(0,0);
    splitter->setStretchFactor(1,1);
    l->setStretchFactor(splitter,1);
    l->addWidget(m_statusBar);

    this->setLayout(l);

    this->resize(1024,800);
    this->setMinimumSize(300,300);

    QList<QAction*> list=m_scripteView->getActions();
    QAction *ac;


    ac =new QAction(QIcon(":/images/fitinscreen.png"),tr("&Find"),this);
    ac->setShortcut(QKeySequence::Find);
    connect(ac,SIGNAL(triggered()),this,SLOT(showFindWidget()));
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);

    ac=new QAction(QIcon(":/images/filesave_s.png"),tr("Save"),this);
    connect(ac,SIGNAL(triggered()),this,SLOT(save()));
    ac->setShortcut(QKeySequence::Save);
    list.append(ac);

    ac=new QAction(this);
    ac->setSeparator(true);
    list.append(ac);
    m_toolBar->addButtonActions(list);
    m_toolBar->setLabelWidget(m_infoWidget);
    m_statusBar->setSizePolicy(QSizePolicy::Ignored,QSizePolicy::Fixed);
    m_statusBar->setSizeGripEnabled(false);



    if(m_property->object()->metaObject()->className()==QString("QFormWidget"))
    {
        m_title=QString("%1---%2").arg(m_property->object()->property("pageName").toString()).arg(m_property->name());
    }
    else
    {
        m_title=QString("%1---%2").arg(m_property->object()->property("objectName").toString()).arg(m_property->name());
    }
    setWindowTitle(m_title);
    m_scripteView->setUndoRedoEnabled(false);
    m_scripteView->insertPlainText(m_property->value().toString());
    m_scripteView->setUndoRedoEnabled(true);
    connect(m_scripteView,SIGNAL(textChanged()),this,SLOT(dataChanged()));
    connect(m_findWidget,SIGNAL(find(QString)),this,SLOT(find(QString)));
    connect(m_findWidget,SIGNAL(findNext()),m_scripteView,SLOT(findNext()));
    connect(m_findWidget,SIGNAL(findPrev()),m_scripteView,SLOT(findPrev()));
    connect(m_findWidget,SIGNAL(replace(QString)),m_scripteView,SLOT(replace(QString)));
    connect(m_findWidget,SIGNAL(replaceAll(QString)),m_scripteView,SLOT(replaceAll(QString)));
    connect(m_findWidget,SIGNAL(replaceAndNext(QString)),m_scripteView,SLOT(replaceNext(QString)));
    connect(m_objectListView,SIGNAL(selectFunction(QString,int,QString)),this,SLOT(functionSelect(QString,int,QString)));

    m_scripteView->setFocus();
}

void QScriptEditDialog::functionSelect(const QString &text, int count,const QString &type)
{
    QString str;
    str=text+"(";
    if(count>0)
    {
        str+=" ";
    }
    for(int i=1;i<count;i++)
    {
        str+=", ";
    }
    if(count==1)
    {
        str+=" ";
    }
    str+=")";
    if(type=="")
    {
        str+=";";
        if(count==0)
        {
            str+="\n";
        }
    }
    m_scripteView->insertPlainText(str);
    m_scripteView->setFocus();

    if(count>0)
    {
        int num=2*count;
        if(count>1)
        {
            num--;
        }
        if(type=="")
        {
            num++;
        }
        for(int i=0;i<num;i++)
        {
            m_scripteView->moveCursor(QTextCursor::Left);
        }
    }
}

void QScriptEditDialog::dataChanged()
{
    QString str=m_scripteView->toPlainText();
    m_modified=(m_property->value().toString()!=str);
    setWindowTitle(m_title+(m_modified?"*":""));
}

void QScriptEditDialog::closeEvent(QCloseEvent *e)
{
    if(m_modified)
    {
        if(QMessageBox::warning(this,tr("Warning"),tr("Script is modified, are you want to save?"),QMessageBox::Yes | QMessageBox::No)==QMessageBox::Yes)
        {
            save();
        }
    }
    QDialog::closeEvent(e);
}

void QScriptEditDialog::save()
{
    m_modified=false;
    setWindowTitle(m_title);
    emit save(m_scripteView->toPlainText());
}

void QScriptEditDialog::showFindWidget()
{
    QTextCursor t=m_scripteView->textCursor();
    if(t.hasSelection())
    {
        m_findWidget->setFindText(t.selectedText());
    }
    m_findWidget->showFindWidget();
}

void QScriptEditDialog::find(const QString &text)
{
    m_scripteView->selectIndex(text);
}
